Other Operators

The following operators are used when working with arrays, expressions, or methods.

Operator

Description

Examples

[ ]

Array concatenation

The expression [A,B] is an array formed by concatenating A and B, which can be scalars or arrays, along the first dimension.

To concatenate second and third levels, nest the brackets; [[1,2],[3,4]] is a 2-element by 2-element array with the first row containing 1 and 2 and the second row containing 3 and 4. Operands must have compatible dimensions; all dimensions must be equal except the dimension that is to be concatenated, e.g., [2,INTARR(2,2)] are incompatible.

Define C as three-point vector:

C = [0, 1, 3]

Add 5 to the end of C:

PRINT, [C, 5]

IDL Prints: 0 1 3 5

Insert -1 at the beginning of C:

PRINT, [-1, C]

IDL Prints: -1 0 1 3

Plot ARR2 appended to ARR1.

PLOT, [ARR1, ARR2]

Define a 3x3 matrix.

KER = [[1,2,1], [2,4,2], $

   [1,2,1]]

Note: Array concatenation is a relatively inefficient operation, and should only be performed once for a given set of data if possible.

Enclose array subscripts

Note: See Array Subscript Syntax: [ ] vs. ( ) for additional details.

A = [2, 1, 5]

Print the 3rd element in A:

PRINT, A[2]

IDL Prints: 5

Note: If one or both of the operands are objects, the operator may be overloaded.

[ : : ]

Colon operator

The colon operator can be used to construct arrays of increasing (or decreasing) values.

The operator has the following syntax:

Result = [start: finish]

or

Result = [start: finish: increment]

See Creating Arrays for details.

PRINT, [0:1:0.1]

IDL prints:

0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

PRINT, [0:10]

IDL prints:

0 1 2 3 4 5 6 7 8 9 10

( )

Group expressions to control order of evaluation or enclose function parameter lists

Note: See Operator Precedence for details on order of evaluation

PRINT, 3 + 4 * 2 ^ 2 /2

IDL Prints: 11

PRINT, (3 + (4 * 2) ^ 2 / 2)

IDL Prints: 35

Enclose function argument lists:

SIN(ANG * PI/180.)

?:

Conditional expression

Provides a way to write simple constructions of the IF...THEN...ELSE statement in expression form.

See Working with Conditional Expressions below.

For

value = expr1 ? expr2 : expr3

expr1 is evaluated first. If expr1 is true, then value = expr2. If expr1 is false, value = expr3.

A=6 & B=4

Set Z to the greater of A and B:

Z = (A GT B) ? A : B

PRINT, Z

IDL Prints: 6

.

Method invocation

Calls an object method. See Acting on Objects Using Methods for more information.

oWindow.Draw

where oWindow is an IDLgrWindow object and Draw is the object method.

->

Method invocation

Calls an object method. See Acting on Objects Using Methods for more information.

oWindow->Draw

where oWindow is an IDLgrWindow object and Draw is the object method.

Working with Conditional Expressions

The conditional expression—written with the ternary operator ?:—has the lowest precedence of all the operators. It provides a way to write simple constructions of the IF...THEN...ELSE statement in expression form. In the following example, Z receives the larger of the values contained by A and B:

IF (A GT B) THEN Z = A ELSE Z = B

This statement can be written more concisely using a conditional expression:

Z = (A GT B) ? A : B

The general form of a conditional expression is:

expr1 ? expr2 : expr3

The expression expr1 is evaluated first. If expr1 is true, then the expression expr2 is evaluated and set as the value of the conditional expression. If expr1 is false, expr3 is evaluated and set as the value of the conditional expression. Only one of expr2 or expr3 is evaluated, based on the result of expr1. (See Definition of True and False for details on how the “truth” of an expression is determined.)

Note: Since ?: has very low precedence—just above assignment—parentheses are not necessary around expr1. However, parentheses are often used in this situation, as they enhance the readability of the expression.